package com.choosefine.paycenter.webgateway.account.web.controller;

import com.choosefine.paycenter.account.dto.AccountClearNameAndMobileDto;
import com.choosefine.paycenter.account.dto.AccountDto;
import com.choosefine.paycenter.account.dto.AccountModifyNameAndMobileDto;
import com.choosefine.paycenter.account.dto.ShiXiaoBaoTrxDto;
import com.choosefine.paycenter.account.model.Account;
import com.choosefine.paycenter.account.model.ShiXiaoBaoTrx;
import com.choosefine.paycenter.account.service.AccountService;
import com.choosefine.paycenter.common.dto.JSONResult;
import com.choosefine.paycenter.common.utils.JSONResultGenerator;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * 资金账户
 * Created by Jay Chang on 2017/3/4.
 */
@Api(value = "资金账户",description = "张洁")
@RestController
@RequestMapping("account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    /**
     * 新增资金账户
     * @param accountDto
     * @return
     */
    @ApiOperation(value = "新增资金账户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", dataType = "String", paramType = "body", required = true, value = "租户编码"),
            @ApiImplicitParam(name = "realName", dataType = "String", paramType = "body", required = true, value = "个人姓名或公司名称"),
            @ApiImplicitParam(name = "roleId", dataType = "Long", paramType = "body", required = true, value = "角色id"),
            @ApiImplicitParam(name = "roleName", dataType = "String", paramType = "body", required = true, value = "角色名称"),
            @ApiImplicitParam(name = "mobile", dataType = "String", paramType = "body", required = true, value = "手机号码"),
    })
    @PostMapping
    public JSONResult addAccount(@Validated({AccountService.AccountAdd.class}) @RequestBody AccountDto accountDto){
        Long id = accountService.saveAccount(accountDto);
        return JSONResultGenerator.buildCreatedSuccessResult(id);
    }

    /**
     * 根据租户编码查询资金账户详情
     * @param userCode
     * @return
     */
    @ApiOperation("根据租户编码查询资金账户详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", dataType = "String", paramType = "header", required = true, value = "租户编码"),
    })
    @GetMapping("detail")
    public JSONResult findAccountByUserCode(@RequestHeader(value="userCode") @NotBlank String userCode){
        Account account = accountService.findByUserCode(userCode);
        return JSONResultGenerator.buildQuerySuccessResult(account);
    }

    /**
     * 总账户余额
     * @return
     */
    @ApiOperation("总账户余额")
    @GetMapping("totalAmount")
    public JSONResult findTotalAmount(){
        return JSONResultGenerator.buildQuerySuccessResult(accountService.findTotalAmount());
    }

    /**
     * 根据资金账户名查询租户编号
     * @param userCode
     * @param accountName
     * @return
     */
    @ApiOperation("根据其他资金账户的账户名查询他的租户编号")
    @GetMapping("other/userCode")
    public JSONResult findUserCodeByAccountName(@RequestHeader("userCode") @NotBlank String userCode,@RequestParam("accountName")String accountName){
        return JSONResultGenerator.buildQuerySuccessResult(accountService.findUserCodeByAccountName(accountName));
    }

    /**
     * 清空资金账户支付密码
     * @param userCode
     * @return
     */
    @ApiOperation("清空资金账户支付密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", dataType = "String", paramType = "header", required = true, value = "租户编码"),
    })
    @PutMapping("payPass/clear")
    public JSONResult clearPayPass(@RequestHeader("userCode") @NotBlank String userCode){
        return JSONResultGenerator.buildOperSuccessResult(accountService.clearPayPass(userCode));
    }

    /**
     * 绑定手机号
     * @param userCode
     * @return
     */
    @ApiOperation("绑定手机号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", dataType = "String", paramType = "header", required = true, value = "租户编码"),
            @ApiImplicitParam(name = "mobile", dataType = "String", paramType = "query", required = true, value = "手机号"),
    })
    @PutMapping("bindMobile")
    public JSONResult bindMobile(@RequestHeader("userCode") @NotBlank String userCode,@RequestParam("mobile") @NotBlank String mobile){
        return JSONResultGenerator.buildOperSuccessResult(accountService.bindMobile(userCode,mobile));
    }

    /**
     * 初始化支付密码
     * @param accountDto
     * @return
     */
    @ApiOperation("初始化支付密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", dataType = "String", paramType = "header", required = true, value = "租户编码"),
            @ApiImplicitParam(name = "payPass", dataType = "String", paramType = "body", required = true, value = "支付密码"),
    })
    @PutMapping("payPass/init")
    public JSONResult initPayPass(@RequestHeader("userCode") @NotBlank String userCode,@RequestBody @Validated({AccountService.InitPayPass.class}) AccountDto accountDto){
        accountDto.setUserCode(userCode);
        int result = accountService.initPayPass(accountDto);
        return JSONResultGenerator.buildOperSuccessResult(result);
    }

    @ApiOperation("验证支付密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", dataType = "String", paramType = "header", required = true, value = "租户编码"),
            @ApiImplicitParam(name = "payPass", dataType = "String", paramType = "body", required = true, value = "支付密码"),
    })
    @PutMapping("payPass/verify")
    public JSONResult verifyPayPass(@RequestHeader("userCode")String userCode,@Validated({AccountService.VerifyPayPass.class}) @RequestBody AccountDto accountDto){
        accountService.verifyPayPass(userCode,accountDto.getPayPass());
        return JSONResultGenerator.buildOperSuccessResult();
    }

    /**
     * 修改密码
     * @return
     *
     */
    @ApiOperation("修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", dataType = "String", paramType = "header", required = true, value = "租户编码"),
            @ApiImplicitParam(name = "oldPayPass", dataType = "String", paramType = "body", required = true, value = "旧支付密码"),
            @ApiImplicitParam(name = "payPass", dataType = "String", paramType = "body", required = true, value = "支付密码"),
    })
    @PutMapping("payPass/edit")
    public JSONResult editPayPass(@RequestHeader("userCode")String userCode, @Validated({AccountService.EditPayPass.class}) @RequestBody AccountDto accountDto){
        accountDto.setUserCode(userCode);
        int result = accountService.editPayPass(accountDto);
        return JSONResultGenerator.buildOperSuccessResult(result);
    }

    /**
     * 冻结资金账号
     * @param userCode
     * @return
     */
    @ApiOperation("冻结资金账号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", dataType = "String", paramType = "header", required = true, value = "租户编码")
    })
    @PutMapping("block")
    public JSONResult blockAccount(@RequestHeader("userCode") @NotBlank  String userCode){
        int result = accountService.blockAccount(userCode);
        return JSONResultGenerator.buildOperSuccessResult(result);
    }

    /**
     * 解冻资金账号
     * @param userCode
     * @return
     */
    @ApiOperation("解冻资金账号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", dataType = "String", paramType = "header", required = true, value = "租户编码"),
    })
    @PutMapping("unBlock")
    public JSONResult unBlockAccount(@RequestHeader("userCode") @NotBlank String userCode){
        int result = accountService.unBlockAccount(userCode);
        return JSONResultGenerator.buildOperSuccessResult(result);
    }

    /**
     * 分页多条件查询资金账户
     * @param accountDto
     * @return
     */
    @ApiOperation("分页多条件查询资金账户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", dataType = "String", paramType = "query", required = false, value = "租户编码"),
            @ApiImplicitParam(name = "realName", dataType = "String", paramType = "query", required = false, value = "租户名称"),
            @ApiImplicitParam(name = "roleId", dataType = "Long", paramType = "query", required = false, value = "角色id"),
            @ApiImplicitParam(name = "status", dataType = "String",allowableValues = "NORMAL,BLOCKED",paramType = "query", required = false, value = "账户状态：NORMAL(正常)，BLOCKED(冻结)"),
            @ApiImplicitParam(name = "pageNum", dataType = "Long", paramType = "query", required = false, value = "页码",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", dataType = "Long", paramType = "query", required = false, value = "每页显示",defaultValue = "10"),
            @ApiImplicitParam(name = "accountDto", dataType = "String", paramType = "query", required = false, value = "自动生成不填写")
    })
    @GetMapping
    public JSONResult findAllByPage(AccountDto accountDto){
        PageInfo<Account> pageInfo = accountService.findAllByPage(accountDto);
        return JSONResultGenerator.buildQuerySuccessResult(pageInfo);
    }

    /**
     * 查询某个资金账户的余额
     * @param userCode
     * @return
     */
    @ApiOperation("根据accountId查询某个资金账户的余额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", dataType = "String", paramType = "header", required = true, value = "租户编码"),
    })
    @GetMapping("balance")
    public JSONResult findBalanceById(@RequestHeader("userCode") @NotBlank  String userCode){
        BigDecimal balance = accountService.findBalanceByUserCode(userCode);
        return JSONResultGenerator.buildQuerySuccessResult(balance);
    }


    /**
     * 查询某个资金账户的租户编码查询其账户id
     * @param userCode
     * @return
     */
    @ApiOperation("查询某个资金账户的租户编码查询资金账户的id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", dataType = "String", paramType = "header", required = true, value = "租户编码"),
    })
    @GetMapping("/accountId")
    public JSONResult findAccountIdByUserCode(@RequestHeader("userCode") @NotBlank String userCode){
        Long id = accountService.findAccountIdByUserCode(userCode);
        return JSONResultGenerator.buildQuerySuccessResult(id);
    }

    /**
     * 施小包流水
     */
    @ApiOperation("施小包流水")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", dataType = "Long", paramType = "query", required = false, value = "开始时间"),
            @ApiImplicitParam(name = "endTime", dataType = "Long", paramType = "query", required = false, value = "截止时间"),
            @ApiImplicitParam(name = "fundFlowDirection",allowableValues = "TRANSFER,RECHARGE,WITHDRAW",dataType = "String", paramType = "query", required = false, value = "资金来源/去向：TRANSFER(转账),RECHARGE(充值),WITHDRAW(提现)"),
    })
    @GetMapping("shixiaobao/trx")
    public JSONResult findShiXiaoBaoTrxHistoryByPage(ShiXiaoBaoTrxDto shiXiaoBaoTrxDto){
        PageInfo<ShiXiaoBaoTrx> pageInfo = accountService.findShiXiaoBaoTrxHistoryByPage(shiXiaoBaoTrxDto);
        return JSONResultGenerator.buildQuerySuccessResult(pageInfo);
    }

    @ApiOperation("检验资金账户名是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accountName", dataType = "String", paramType = "query", required = false, value = "账户名"),
    })
    @GetMapping("exists")
    public JSONResult findAccountIsExists(@RequestHeader("userCode") String userCode,@RequestParam("accountName") String accountName){
        accountService.checkAccountNameExists(accountName);
        return JSONResultGenerator.buildOperSuccessResult();
    }


    @ApiOperation("清空资金账号的账户名以及手机号码(用于删除登录账号时候的操作)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", dataType = "String", paramType = "body", required = true, value = "手机号码"),
            @ApiImplicitParam(name = "userList.userCode", dataType = "String", paramType = "body", required = true, value = "租户编码"),
            @ApiImplicitParam(name = "userList.roleId", dataType = "Long", paramType = "body", required = true, value = "角色id"),
    })
    @PutMapping("clearAccountNameAndMobile")
    public JSONResult clearAccountNameAndMobile(@RequestBody AccountClearNameAndMobileDto accountClearNameAndMobileDto){
        accountService.clearAccountNameAndMobile(accountClearNameAndMobileDto);
        return JSONResultGenerator.buildOperSuccessResult();
    }

    @ApiOperation("更改资金账号绑定的手机号码及账户名(用于客服平台更换登录账号的手机号的时候，需要将这些租户对应的资金账号)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", dataType = "String", paramType = "body", required = true, value = "手机号码"),
            @ApiImplicitParam(name = "userList.userCode", dataType = "String", paramType = "body", required = true, value = "租户编码"),
            @ApiImplicitParam(name = "userList.roleId", dataType = "Long", paramType = "body", required = true, value = "角色id"),
    })
    @PutMapping("modifyAccountNameAndMobile")
    public JSONResult modifyAccountNameAndMobile(@RequestBody AccountModifyNameAndMobileDto accountModifyNameAndMobileDto){
        accountService.modifyAccountNameAndMobile(accountModifyNameAndMobileDto);
        return JSONResultGenerator.buildOperSuccessResult();
    }
}
